package com.learning.concurrent.basic.designpattern.masterworker;

import lombok.Data;

import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentLinkedQueue;

/**
 * description:
 *
 * @author minghuiZhang
 * @date created in  15:01 2018/5/15
 * modified by
 */
@Data
public class Worker implements Runnable{
	@Override
	public void run() {
		while(true){
			Task task = this.workerQueue.poll();
			if (task == null){
				break;
			}
			//业务处理
			Object result = handler(task);
			this.resultMap.put(Integer.toString(task.getId()),result);
		}
	}

	private Object handler(Task task) {
		Object result = null;
		try {
			//表示处理task任务的耗时，可能是数据的加工，也可能是操作数据库
			Thread.sleep(500);
			result = task.getPrice();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		return result;
	}

	private ConcurrentLinkedQueue<Task> workerQueue;
	private ConcurrentHashMap<String,Object> resultMap;
}
